<?php

namespace App\Service\User\v1_3_3;

use App\Constants\ErrorCode;
use App\Model\UserSocialProfile;
use App\Service\BaseService;

/**
 * 社交资料编辑
 */
class SocialProfileModifyService extends BaseService
{
    public function handle()
    {
        $userId = $this->getUserId();
        $fields = array_keys(UserSocialProfile::PROFILE_TYPE);
        $allPost = $this->request->all();
        $param = $this->checkParam($allPost, $fields);
        if (empty($param)) {
            return $this->setError(ErrorCode::PARAMETER_ERROR, '参数缺失，请重新提交～');
        }

        //更新社交资料
        $ret = UserSocialProfile::updateProfile($userId, $param);
        return ['result' => 1];
    }

    /**
     * 是否参数没传
     */
    function checkParam($allPost = [], $fields = [])
    {
        $param = [];
        foreach ($fields as $field) {
            if (isset($allPost[$field]) && $allPost[$field]) $param[$field] = $allPost[$field];
        }
        return $param;
    }
}
